#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "public.h"
#include "./components/chatmessagewidget/chatmessagewidget.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    // 加载头像（示例使用内置图标）
    resize(QT_WIDTH, QT_HEIGHT);
    // 禁止窗口修改大小
    setFixedSize(QT_WIDTH, QT_HEIGHT);
    // 设置窗口背景颜色为白色
    setStyleSheet("background-color: #f5f5f5;");
    // 绘制侧边栏
    dorweSideBar();
    // 绘制消息列表
    dorweMessageList();
    // 初始化聊天窗口
    dorweChatWindow();
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::updateSideBar()
{
    // 绘制用户头像
    myHeadImg = new QLabel(this);
    myHeadImg->setGeometry(9, 30, 40, 40);
    QString myHeadImgPath;
    if (headImgType < 4)
    {
        myHeadImgPath = QString("background-image: url(../weChat/images/head%1.png);").arg(headImgType);
    }
    else
    {
        myHeadImgPath = QString("background-image: url(../weChat/images/head4.png);");
    }
    myHeadImg->setStyleSheet(myHeadImgPath);
    myHeadImg->show();
    this->update();     // 强制刷新界面
}
void MainWindow::dorweSideBar()
{
    QLabel *SideBar = new QLabel(this);
    SideBar->setGeometry(0, 0, QT_SIDEBAR_WIDTH, QT_HEIGHT);
    SideBar->setStyleSheet("background-color:#2e2e2e;background-image: url(../weChat/images/ceBianLan.png);");

    // 绘制关不掉的窗口
    QPushButton *actionButton = new QPushButton(this);
    actionButton->setGeometry(9, 302, 40, 40);
    actionButton->setStyleSheet("background-color: rgba(0,0,0,0);");
    connect(actionButton, &QPushButton::clicked, [=]()
            {
        emit toGamesPageFn();
        qDebug() << "点击了游戏按钮" << endl; });
    // 绘制关不掉的窗口
    QPushButton *actionButton2 = new QPushButton(this);
    actionButton2->setGeometry(9, 213, 40, 40);
    actionButton2->setStyleSheet("background-color: rgba(255,0,0,0);");
    connect(actionButton2, &QPushButton::clicked, [=]()
            { tcpFn->tcp_send(1001); }); // 使用 Qt::QueuedConnection
}

void MainWindow::mousePressEvent(QMouseEvent *event)
{
    // Qt::MouseButton btn = event->button();
    // if (Qt::LeftButton == btn)
    // {
    //     qDebug() << "鼠标左键按下" << event->x() << ","
    //              << event->y() << endl;

    //     qDebug() << "鼠标左键按下全局坐标" << event->globalX() << "," << event->globalY() << endl;

    //     Qt::KeyboardModifiers modifiers = event->modifiers();
    //     if (Qt::ControlModifier & modifiers)
    //     {
    //         qDebug() << "同时按下了ctrl键" << endl;
    //     }
    // }

    // if (Qt::RightButton == btn)
    // {
    //     qDebug() << "鼠标右键按下" << event->x() << ","
    //              << event->y() << endl;
    // }

    // if (Qt::MidButton == btn)
    // {
    //     qDebug() << "鼠标中键按下" << event->x() << ","
    //              << event->y() << endl;
    // }
}

// 鼠标移动
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    // qDebug() << "鼠标移动" << event->x() << ","
    //          << event->y() << endl;
}

// 鼠标松开
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    // qDebug() << "鼠标松开" << event->x() << ","
    //          << event->y() << endl;
}

// 鼠标双击
void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
    // qDebug() << "鼠标双击" << event->x() << ","
    //          << event->y() << endl;
}

void MainWindow::enterEvent(QEvent *event)
{

    // qDebug() << "鼠标进入窗口" << endl;
}

void MainWindow::leaveEvent(QEvent *event)
{
    // qDebug() << "鼠标离开" << endl;
}
